<html>
<header>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style>
    body{
      margin: 0px;
    }
  </style>
</header>

<body>
  <div class="form">
    <div id="iframeEditor"></div>
  </div>
</body>

</html>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
  var newurl= ''
    newurl=getUrlParam('urldata')
  $(function () {
    var url = newurl+ '/api/fd/document/getDocument'
    var documentId = getUrlParam('documentId')
    var mode = getUrlParam('mode')
    var data
    if (mode && mode == 'edit') {
      data = {
        documentId: documentId,
        mode: "edit"
      }
    }else{
      data = {
        documentId: documentId,
        mode: "view"
      }
    }
    requestData(url, data, res => {
      if (res.code == 0) {
        alert("找不到文件实体信息")
      }
      let scriputDom = document.createElement("script");
      scriputDom.onload = () => {
          var conf = JSON.parse(res.data.config)
          config = Object.assign({}, config, conf)
          сonnectEditor();
      }
      scriputDom.src = res.data.api;
      document.body.appendChild(scriputDom);
    })
  })

  function requestData(url, data, success, error) {
    var token = getUrlParam('token')
    $.ajax({
      url: url,
      type: 'post',
      dataType: 'json',
      data: JSON.stringify(data),
      cache: false,
      headers: {
        "Authorization": token,
        'Content-Type': 'application/json'  //multipart/form-data;boundary=--xxxxxxx   application/json
      },
      success,
      error
    });
  }



  /**
   * 输入文档名称，如果为空，重复弹窗
   * @param name 参数名称
   * @returns {string| 参数值}
   */
  function inpDocumentName() {
    var code = prompt("请输入文档名称");
    if (code) {
      return code
    } else {
      alert("文档名称不可为空，重新输入")
      return inpDocumentName()
    }
  }


  /**
   * 获取地址栏信息
   * @param name 参数名称
   * @returns {string| 参数值}
   */
  function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
  }
  var config = {}
  var docEditor;

  var innerAlert = function (message) {
    if (console && console.log) console.log(message);
  };

  var onAppReady = function () {
    innerAlert("Document editor ready");
  };

  var onDocumentStateChange = function (event) {
    var title = document.title.replace(/\*$/g, "");
    document.title = title + (event.data ? "*" : "");
  };

  var onRequestEditRights = function () {
    location.href = location.href.replace(RegExp("mode=view&?", "i"), "");
  };

  var onError = function (event) {
    if (event) innerAlert(event.data);
  };

  var onOutdatedVersion = function (event) {
    location.reload(true);
  };

  var replaceActionLink = function (href, linkParam) {
    var link;
    var actionIndex = href.indexOf("&actionLink=");
    alert(actionIndex);
    if (actionIndex != -1) {
      var endIndex = href.indexOf("&", actionIndex + "&actionLink=".length);
      if (endIndex != -1) {
        link =
          href.substring(0, actionIndex) +
          href.substring(endIndex) +
          "&actionLink=" +
          encodeURIComponent(linkParam);
      } else {
        link =
          href.substring(0, actionIndex) +
          "&actionLink=" +
          encodeURIComponent(linkParam);
      }
    } else {
      alert(linkParam);
      link = href + "&actionLink=" + encodeURIComponent(linkParam);
    }
    alert(href);
    alert(link);
    return link;
  };

  var onMakeActionLink = function (event) {
    alert();
    var actionData = event.data;
    alert(actionData);
    var linkParam = JSON.stringify(actionData);
    alert(linkParam);
    docEditor.setActionLink(replaceActionLink(location.href, linkParam));
  };
  var onRequestSendNotify = function (event) {
    var ACTION_DATA = event.data.actionLink;
    var comment = event.data.message;
    var emails = event.data.emails;
  };

  var onCollaborativeChanges = function () {
    alert("bierengaine");
  };

  var onDocumentReady = function () { };

  var onRequestUsers = function () {
    docEditor.setUsers({
      users: [
        {
          email: "john@example.com",
          name: "John Smith",
        },
        {
          email: "kate@example.com",
          name: "Kate Cage",
        },
      ],
    });
  };

  var config = this.config;
  config.width = "100%";
  config.height = "100%";
  config.events = {
    onAppReady: onAppReady,
    onDocumentStateChange: onDocumentStateChange,
    onRequestEditRights: onRequestEditRights,
    onError: onError,
    onOutdatedVersion: onOutdatedVersion,
    onMakeActionLink: onMakeActionLink,
    onRequestSendNotify: onRequestSendNotify,
    onCollaborativeChanges: onCollaborativeChanges,
    onDocumentReady: onDocumentReady,
    onRequestUsers: onRequestUsers,
  };

  var сonnectEditor = function () {
    docEditor = new DocsAPI.DocEditor("iframeEditor", config);
  };

  if (window.addEventListener) {
    // window.addEventListener("load", сonnectEditor);
  } else if (window.attachEvent) {
    // window.attachEvent("load", сonnectEditor);
  }

  console.log("hello world")
</script>
